package com.wmc;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Random;

import com.commonfunction.CommFunction;
import com.databasefunction.AllDBFunction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class WeighMachineReview extends ActionSupport{
	private String rstno,vehicleno,custname,address,commodity,brokername,ponum,nobags,grossweight,sysdate,tareweight,systime,netweight,userid,commisionname;
	boolean isBroker = false;
	String barcode;
	String userrole;
	
	HashMap dataMap = new HashMap();
	
	public String getRstno() {
		return rstno;
	}

	public void setRstno(String rstno) {
		this.rstno = rstno;
	}

	public String getVehicleno() {
		return vehicleno;
	}

	public void setVehicleno(String vehicleno) {
		this.vehicleno = vehicleno;
	}

	public String getCustname() {
		return custname;
	}

	public void setCustname(String custname) {
		this.custname = custname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCommodity() {
		return commodity;
	}

	public void setCommodity(String commodity) {
		this.commodity = commodity;
	}

	public String getBrokername() {
		return brokername;
	}

	public void setBrokername(String brokername) {
		this.brokername = brokername;
	}

	public String getPonum() {
		return ponum;
	}

	public void setPonum(String ponum) {
		this.ponum = ponum;
	}

	public String getNobags() {
		return nobags;
	}

	public void setNobags(String nobags) {
		this.nobags = nobags;
	}

	public String getGrossweight() {
		return grossweight;
	}

	public void setGrossweight(String grossweight) {
		this.grossweight = grossweight;
	}

	public String getSysdate() {
		return sysdate;
	}

	public void setSysdate(String sysdate) {
		this.sysdate = sysdate;
	}

	public String getTareweight() {
		return tareweight;
	}

	public void setTareweight(String tareweight) {
		this.tareweight = tareweight;
	}

	public String getSystime() {
		return systime;
	}

	public void setSystime(String systime) {
		this.systime = systime;
	}

	public String getNetweight() {
		return netweight;
	}

	public void setNetweight(String netweight) {
		this.netweight = netweight;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getCommisionname() {
		return commisionname;
	}

	public void setCommisionname(String commisionname) {
		this.commisionname = commisionname;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		AllDBFunction allDBFunction = new AllDBFunction();
		userrole = (String) ActionContext.getContext().getSession().get("userrole"); 
		if ((null != userrole) && (userrole.equals("weightmachine") || userrole.equals("admin") || userrole.equals("superadmin"))) {
			
		}else
			return "unauthorised";
		
		
		CommFunction commFunction = new CommFunction();
		/*
		if (isBroker) {
			barcode = randomString(2)+commFunction.getUniqueId(brokername)+randomString(2)+commFunction.getUniqueId(custname)+ponum;
		} else {
			barcode = randomString(2)+commFunction.getUniqueId(commisionname)+randomString(2)+commFunction.getUniqueId(custname)+ponum;	
		}
		*/
		
		do{
		barcode = randomString(10);
		System.out.println("Barcode :"+barcode);
		}while(allDBFunction.isBarcodeExists(barcode));
		
		
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
		SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("HH:mm");
		if(null != ponum){
		dataMap.put("rstno",rstno );
		dataMap.put("vehicleno", vehicleno);
		dataMap.put("custname", custname);
		dataMap.put("address",address );
		dataMap.put("commodity", commodity);
		dataMap.put("brokername",brokername);
		dataMap.put("commisionname",commisionname );
		dataMap.put("ponum",ponum );
		dataMap.put("nobags",nobags );
		dataMap.put("grossweight", grossweight);
		dataMap.put("sysdate",simpleDateFormat.format(calendar.getTime()));
		dataMap.put("systime",  simpleTimeFormat.format(calendar.getTime()));
		dataMap.put("tareweight",tareweight );
		dataMap.put("netweight", netweight);
		dataMap.put("userid", userid);
		dataMap.put("barcode", barcode);
		ActionContext.getContext().getSession().put("datamap", dataMap);
		}
		return "success";
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		userrole = (String) ActionContext.getContext().getSession().get("userrole");
		
		try{
			AllDBFunction allDBFunction = new AllDBFunction();
			if (!brokername.trim().equals("")) {
				isBroker = true;
				if (!allDBFunction.isdealerexists(custname) ) {
					//add error
					addActionError("Customer not present");
				}else if (!allDBFunction.isdealerexists(brokername)) {
					//add error
					
					addActionError("Brocker Name not present");
				}else{
					address = allDBFunction.getaddress(custname);
				}
			}else if (!commisionname.trim().equals("")){
				isBroker = false;
				if (!allDBFunction.isdealerexists(commisionname)) {
					//add error
					addActionError("Commision Agent Name not present");
				}else{
					if (custname.trim().equals("")) {
						address = allDBFunction.getaddress(commisionname);
					} else {
						address = allDBFunction.getaddress(custname);	
					}
				}
			}else{
				addActionError("Brocker/Commision Agent Name not present");
			}
			
			}catch(Exception e){
				System.out.println("Validation error with");
				e.printStackTrace();
			}
		}
	
	
	String randomString( int len ) 
	{
		final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		Random rnd = new Random();

		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++)
			sb.append(AB.charAt(rnd.nextInt(AB.length())));
		return sb.toString();
	}
		
}
